package com.yanqu.road.server.cmd.monopolymarket;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description
 * @Author cwq
 * @Data 2021/8/20 10:25
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_ENTER_FROM_CROSS, desc = "进入榷场  跨服返回区服 协议")
public class MonopolyMarketEnterFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketEnterRespMsgToGameServerMsg resp = MonopolyMarketProto.MonopolyMarketEnterRespMsgToGameServerMsg.parseFrom(packet.getBytes());

        MonopolyMarketProto.MonopolyMarketEnterRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketEnterRespMsg.newBuilder();
        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
        marketModule.inSystemId = 0;
        // 榷署弹窗
        if(marketModule.baseData.getOfficeTip() != null){
            MonopolyMarketProto.MonopolyMarketOfficeTipEntity.Builder officeTip = MonopolyMarketProto.MonopolyMarketOfficeTipEntity.newBuilder();
            officeTip.setRobName(marketModule.baseData.getOfficeTip().robName);
            officeTip.setLevel(marketModule.baseData.getOfficeTip().level);
            officeTip.setNo(marketModule.baseData.getOfficeTip().no);
            builder.setOfficeTip(officeTip);
            marketModule.baseData.setOfficeTip(null);
        }
        // 资源战弹窗
        for(MonopolyMarketProto.MonopolyMarketSynPointSettlementToGameServerMsg item :  resp.getPointSettlementListList()){
            MonopolyMarketMgr.leaveResGrabPoint(player,item.getType(),item.getPosIndex(),false);
            // 加经验
            Property property = MonopolyMarketMgr.addReward(item.getType(),item.getRewardNum());
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.MonopolyMarket,eLogMoneyType.MonopolyMarketResGrabEnterSystemReward);
            // 弹窗
            MonopolyMarketProto.MonopolyMarketResGrabTipEntity.Builder tip = MonopolyMarketMgr.builderMonopolyMarketResGrabTipEntity(item);
            builder.addResGrabTip(tip);
        }
        // 要塞奖励弹窗
        for(MonopolyMarketProto.MonopolyMarketFortressTipEntity item :  resp.getFortressSettlementListList()){
            Property property = PropertyHelper.parseStringToProperty(item.getReward());
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.MonopolyMarket,eLogMoneyType.MonopolyMarketFortressReceiveCollectReward);
        }
        builder.addAllFortressTip(resp.getFortressSettlementListList());
        marketModule.baseData.setHasTip(false);
        MonopolyMarketMgr.refreshUser(player,false,true);

        builder.setRet(ret);
        player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_ENTER, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
